{"componentChunkName":"component---src-templates-post-jsx","path":"/[CS]배열/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"3fe44622-13ba-589a-bbe8-01581ea7eb50","excerpt":"🧷 배열 (Array) 동일한 자료형의 데이터를 연속된 공간에 저장하기 위한 자료구조이다. 연관된 데이터를 그룹화하여 묶어준다. 🪚 장점 연관된 데이터를 저장하기 위한 변수의 선언을 줄여준다. 반복문 등을 이용하여 계산 과정을 쉽게 처리할 수 있다. 🪚 배열 회전 프로그램 기본적인 회전 알고리즘  에 첫 번째 인덱스의 값을 저장하고, 나머지 값들을 한 칸…","html":"<h2>🧷 배열 (Array)</h2>\n<p>동일한 자료형의 데이터를 연속된 공간에 저장하기 위한 자료구조이다. <strong>연관된 데이터를 그룹화하여 묶어준다.</strong></p>\n<h3>🪚 장점</h3>\n<ul>\n<li>연관된 데이터를 저장하기 위한 변수의 선언을 줄여준다.</li>\n<li>반복문 등을 이용하여 계산 과정을 쉽게 처리할 수 있다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">int[] array = { 1,2,3,4,5,6 };\nint arraySize = array.length;</code>\n        </deckgo-highlight-code>\n<h3>🪚 배열 회전 프로그램</h3>\n<img src='https://t1.daumcdn.net/cfile/tistory/99AFA23F5BE8F31B0C' width='70%'>\n<p><strong>기본적인 회전 알고리즘</strong></p>\n<ul>\n<li><code class=\"language-text\">tmp</code> 에 첫 번째 인덱스의 값을 저장하고, 나머지 값들을 한 칸씩 앞으로 땡겨서 저장한다.</li>\n<li>이후 <code class=\"language-text\">array[n]</code> 에 <code class=\"language-text\">tmp</code> 값을 저장한다.</li>\n</ul>\n<p><strong>저글링 알고리즘</strong></p>\n<ul>\n<li>최대공약수 gcd를 이용해 집합을 나누어 여러 요소를 한꺼번에 이동시킨다.</li>\n</ul>\n<img src='https://cdncontribute.geeksforgeeks.org/wp-content/uploads/arra.jpg' width='80%'>\n<ul>\n<li>주어진 배열이 <code class=\"language-text\">int[] array = {1,2,3,4,5,6,7,8,9,10,11,12}</code> 라면, <code class=\"language-text\">1,2,3</code> 을 뒤로 옮길 때, 인덱스를 3개씩 묶고 회전시키는 방법이다.</li>\n<li><code class=\"language-text\">[]arrary</code> -> <code class=\"language-text\">{4,2,3,7,5,6,10,8,9,1,11,12}</code></li>\n<li><code class=\"language-text\">[]arrary</code> -> <code class=\"language-text\">{4,5,3,7,8,6,10,11,9,1,2,12}</code></li>\n<li><code class=\"language-text\">[]arrary</code> -> <code class=\"language-text\">{4,5,6,7,8,9,10,11,12,1,2,3}</code></li>\n</ul>\n<p><strong>역전 알고리즘</strong></p>\n<ul>\n<li>회전시키는 수에 대해 구간을 나눠 <strong>reverse</strong>로 구현한다.</li>\n</ul>\n<p><code class=\"language-text\">d=2</code> 이면, 구간을 1,2 / 3,4,5,6,7 로 나눈다.</p>\n<ul>\n<li>첫 번째 구간 <code class=\"language-text\">reverse</code> -> 2,1</li>\n<li>두 번째 구간 <code class=\"language-text\">reverse</code> - > 7,6,5,4,3</li>\n<li>합치기 -> 2,1,7,6,5,4,3</li>\n<li>합친 배열을 <code class=\"language-text\">reverse</code> -> 3,4,5,6,7,1,2</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">void reverseArr(int[] array, int start, int end) {\n  while (start &lt; end) {\n    int temp = array[start];\n    array[start] = array[end];\n    array[end] = temp;\n\n    start++;\n    end--;\n  }\n}\n\nvoid rotateLeft(int[] array, int d, int n) {\n  reverseArr(array, 0, d - 1);\n  reverseArr(array, d, n - 1);\n  reverseArr(array, 0, n - 1);\n}</code>\n        </deckgo-highlight-code>\n<h3>🪚 배열의 특정 최대 합 구하기</h3>\n<p><code class=\"language-text\">array[i]</code> 가 있을 때, <code class=\"language-text\">i * array[i]</code> 의 합이 가장 클 때 그 값을 출력하기</p>\n<ul>\n<li>이를 회전을 통해 찾을 수 있다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">// Input\narray = { 1, 20, 2, 10 }\n// Output\n72\n\n2번 회전했을 때 아래와 같이 최댓값이 나온다.\n{ 2, 10, 1, 20 }\n20 * 3 + 1 * 2 + 10 * 1 + 20 * 0 = 72</code>\n        </deckgo-highlight-code>\n<p><strong>접근법</strong></p>\n<ul>\n<li><code class=\"language-text\">array[i]</code> 의 전체 합과 <code class=\"language-text\">i * array[i]</code> 의 전체 합을 저장할 변수 선언</li>\n<li>최종 가장 큰 <code class=\"language-text\">sum</code> 값을 저장할 변수 선언</li>\n<li>배열을 회전시키면서 <code class=\"language-text\">i * array[i]</code> 의 합 중 최댓값을 저장한다.</li>\n</ul>\n<p><strong>해결법</strong></p>\n<ul>\n<li>회전 없이 <code class=\"language-text\">i * array[i]</code> 의 sum을 저장한 값 = <code class=\"language-text\">R0</code></li>\n<li>1번 회전하고 <code class=\"language-text\">i * array[i]</code> 의 sum을 저장한 값 = <code class=\"language-text\">R1</code></li>\n<li>이 두 개를 빼면 <code class=\"language-text\">R1 - R0 = array[0] + array[1] + ... array[n - 2] - (n - 1) * array[n - 1]</code></li>\n<li>이를 계속 반복하다보면, <code class=\"language-text\">Rj - R(j-1) = arrSum - n * array[n - j]</code> 라는 규칙을 얻을 수 있다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">int arrSum = 배열의 총 합\nint maxSum = 회전없이 구한 i * array[i]의 sum;\n    \nfor (int j = 1; j &lt; n; j++){\n  curSum = curSum + arrSum - n*arr[n-j];\n  \n  if ( curSum &gt; maxSum )\n    maxSum = curSum;\n}\n\nreturn maxSum;</code>\n        </deckgo-highlight-code>\n<h3>🪚 특정 배열을 <code class=\"language-text\">array[i] = i</code> 로 재배열</h3>\n<p>주어진 배열에서 <code class=\"language-text\">array[i] = i</code> 가 가능한 것만 재배열시키기 (<code class=\"language-text\">array[i] = i</code> 가 없으면 -1로 채운다.)</p>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">// Input\narray = { -1, -1, 6, 1, 9, 3, 2, -1, 4, -1 }\n// Output\n{ -1, 1, 2, 3, 4, -1, 6, -1, -1, 9 }</code>\n        </deckgo-highlight-code>\n<p><strong>접근법</strong></p>\n<ul>\n<li><code class=\"language-text\">array[i] != -1</code> &#x26;&#x26; <code class=\"language-text\">array[i] != i</code> 가 우선 조건</li>\n<li>해당 값을 저장(<code class=\"language-text\">x</code>)해두고, 이 값이 <code class=\"language-text\">x</code> 일때 <code class=\"language-text\">array[x]</code> 를 탐색</li>\n<li><code class=\"language-text\">array[x]</code> 값을 저장(<code class=\"language-text\">y</code>)해두고, <code class=\"language-text\">array[x]</code> 가 -1이 아니면서 <code class=\"language-text\">array[x]</code> 가 <code class=\"language-text\">x</code> 가 아닌 동안을 탐색</li>\n<li><code class=\"language-text\">array[x]</code> 를 <code class=\"language-text\">x</code> 값으로 저장해주고, 기존의 <code class=\"language-text\">x</code> 를 <code class=\"language-text\">y</code> 로 수정</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">int fix(int[] array, int len) {\n  for (int i = 0; i &lt; len; i++) {\n    if (array[i] != -1 &amp;&amp; array[i] != i) {\n      int x = array[i];\n\n      while (array[x] != -1 &amp;&amp; array[x] != x) {\n        int y = array[x];\n        array[x] = x;\n        x = y;\n      }\n      array[x] = x;\n\n      if (array[i] != i) array[i] = -1;\n    }\n  }\n}</code>\n        </deckgo-highlight-code>\n<hr>\n<h2>📕 참고</h2>\n<ul>\n<li><a href=\"https://gyoogle.dev/blog/computer-science/data-structure/Array.html\">Tech Interview for developer</a></li>\n<li><a href=\"https://github.com/JaeYeopHan/Interview_Question_for_Beginner/tree/master/DataStructure#array-vs-linked-list\">JaeYeopHan/Interview<em>Question</em>for_Beginner</a></li>\n</ul>","frontmatter":{"title":"📂 배열 (Array)","date":"May 18, 2022","update":"May 18, 2022","tags":["개발상식","Java","배열"],"series":"📂 Computer Science"},"fields":{"slug":"/[CS]배열/","readingTime":{"minutes":4.73}}},"seriesList":{"edges":[{"node":{"id":"3fe44622-13ba-589a-bbe8-01581ea7eb50","fields":{"slug":"/[CS]배열/"},"frontmatter":{"title":"📂 배열 (Array)"}}},{"node":{"id":"5b487c3d-e474-5a07-a26c-fc8c7d7cab22","fields":{"slug":"/[CS]연결리스트/"},"frontmatter":{"title":"📂 연결 리스트 (Linked List)"}}},{"node":{"id":"57149a6b-c730-5ce4-a3c5-9cea44979c8c","fields":{"slug":"/[CS]Array&ArrayList&LinkedList/"},"frontmatter":{"title":"📂 Array vs. ArrayList vs. LinkedList"}}},{"node":{"id":"28dac540-50bf-57df-afd9-9561888e8889","fields":{"slug":"/[CS]Stack&Queue/"},"frontmatter":{"title":"📂 Stack & Queue"}}},{"node":{"id":"4c2e96a9-4c71-57e3-b8e4-220b2cdf3002","fields":{"slug":"/[CS]Heap/"},"frontmatter":{"title":"📂 Heap"}}},{"node":{"id":"44f6bf8c-3235-5ff7-9d5f-f7c816218acb","fields":{"slug":"/[CS]Trie/"},"frontmatter":{"title":"📂 Trie(트라이)"}}},{"node":{"id":"81b508c5-2689-54e4-b36f-2e8bd07e2297","fields":{"slug":"/[CS]Tree/"},"frontmatter":{"title":"📂 Tree(트리)"}}},{"node":{"id":"2efb6036-05bf-594e-b9b5-03e399be6f78","fields":{"slug":"/[CS]이진탐색트리/"},"frontmatter":{"title":"📂 이진탐색트리(Binary Search Tree)"}}},{"node":{"id":"d1ac08af-f215-5176-9cd8-df1760f4ff7e","fields":{"slug":"/[CS]BTree&B+Tree/"},"frontmatter":{"title":"📂 B Tree & B+ Tree"}}}]},"previous":{"fields":{"slug":"/[Programmers]17677/"},"frontmatter":{"title":"👩‍💻 17677. 뉴스 클러스터링"}},"next":{"fields":{"slug":"/[CS]연결리스트/"},"frontmatter":{"title":"📂 연결 리스트 (Linked List)"}}},"pageContext":{"id":"3fe44622-13ba-589a-bbe8-01581ea7eb50","series":"📂 Computer Science","previousPostId":"567fc5cf-5a51-5a77-bca1-3389363256bc","nextPostId":"5b487c3d-e474-5a07-a26c-fc8c7d7cab22"}},"staticQueryHashes":[]}